<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Contract</title>
<link href="style.css" rel="stylesheet" type="text/css">
<!--[if lte IE 7]>
<style>
.content { margin-right: -1px; } /* 此 1px 负边距可以放置在此布局中的任何列中，且具有相同的校正效果。 */
ul.nav a { zoom: 1; }  /* 缩放属性将为 IE 提供其需要的 hasLayout 触发器，用于校正链接之间的额外空白 */
</style>
<![endif]-->
<script type="text/javascript" src="lib/fxsupport.js"></script>
<script type="text/javascript" src="lib/jquery-1.8.3.js"></script>
<script type="text/javascript" src="lib/JSResource.js"></script>
<script type="text/javascript" src="lib/urlpara.js"></script>
</head>

<body>

	<div class="container">
		<div class="header">
			<a href="#"><img src="logo.jpg" alt="在此处插入徽标" name="Insert_logo"
				width="20%" height="90" id="Insert_logo"
				style="background-color: #8090AB; display:block;" /> </a>
			<!-- end .header -->
		</div>
		<div class="sidebar1">
			<ul class="nav">
				<li><a href="home.jsp">Department Home</a></li>
				<li><a href="contributorlist.htm">Contributor List</a></li>
				<li><a href="memberlist.htm">Member List</a></li>
			</ul>
			<!-- end .sidebar1 -->
		</div>
		<div class="content">
			<form name="form">
				id: <input type="text" name="id" id="id"><br>
				description: <input type="text" name="description" id="description"><br>
				<!--time: <input type="text" name="time" id="time"><br>-->
				time: <select id="tYEAR" size="1" onChange="createSelect()"></select>
				<select id="tMON" size="1" onChange="createSelect()"></select> <select
					id="tDAY" size="1"></select> <br> amount: <input type="text"
					name="amount" id="amount"><br> type: <select
					name="type" id="type" onChange="setType()">
					<option value="collaborate" selected>collaborate</option>
					<option value="donate">donate</option>
					<option value="endowment">endowment</option>
					<option value="reward">reward</option>
				</select>
				<div id="extenddiv"></div>
				<input type="button" name="commit" id="commit" value="summit"
					onclick="commitPage()">
			</form>
			<!-- end .content -->
		</div>
		<div class="footer">
			<!-- end .footer -->
		</div>
		<!-- end .container -->
	</div>
	<script type="text/javascript">
		function createSelect(ActionFlag) {
			var selYear = document.getElementById("tYEAR");
			var selMonth = document.getElementById("tMON");
			var selDay = document.getElementById("tDAY");
			var dt = new Date();

			if (ActionFlag == 1) {
				MinYear = 1980;
				MaxYear = MinYear + 100;

				for ( var i = MinYear; i <= MaxYear; i++) {
					var op = document.createElement("OPTION");
					op.value = i;
					op.innerHTML = i;
					selYear.appendChild(op);
				}
				selYear.selectedIndex = dt.getFullYear() - 1980;

				for ( var i = 1; i < 13; i++) {
					var op = document.createElement("OPTION");
					op.value = i;
					op.innerHTML = i;
					selMonth.appendChild(op);
				}
				selMonth.selectedIndex = dt.getMonth();
			}

			var date = new Date(selYear.value, selMonth.value, 0);
			var daysInMonth = date.getDate();
			selDay.options.length = 0;

			for ( var i = 1; i <= daysInMonth; i++) {
				var op = document.createElement("OPTION");
				op.value = i;
				op.innerHTML = i;
				selDay.appendChild(op);
			}

			selDay.selectedIndex = dt.getDate() - 1;
		}
		createSelect(1);
		function setType() {
			document.getElementById("extenddiv").innerHTML = "";
			switch (document.getElementById("type").selectedIndex) {
			case 0:
				document.getElementById("extenddiv").innerHTML = "partner: <input type=\"text\" name=\"partner\" id=\"partner\">";
				break;
			case 1:
				document.getElementById("extenddiv").innerHTML = "limit: <input type=\"text\" name=\"limit\" id=\"limit\">";
				break;
			case 2:
				document.getElementById("extenddiv").innerHTML = "capital: <input type=\"text\" name=\"capital\" id=\"capital\"><br>rate: <input type=\"text\" name=\"rate\" id=\"rate\">";
				break;
			case 3:
				document.getElementById("extenddiv").innerHTML = "reason: <input type=\"text\" name=\"reason\" id=\"reason\">";
				break;
			}
		}
		var projectId = getParamFromURL("projectId");
		var id = getParamFromURL("id");
		if (id != "") {
			resource
					.GET(
							"contract/contract/" + id,
							function() {
								document.getElementById("id").value = id;
								document.getElementById("id").disabled = true;
								document.getElementById("description").value = resource
										.getColumnValue("description");
								document.getElementById("amount").value = resource
										.getColumnValue("amount");
								var timepart = resource.getColumnValue("time")
										.split("-");
								document.getElementById("tYEAR").selectedIndex = parseInt(timepart[0]) - 1980;
								document.getElementById("tMON").selectedIndex = parseInt(timepart[1])-1;
								document.getElementById("tDAY").selectedIndex = parseInt(timepart[2])-1;
								for ( var i = 0; i < resource.getChildren().length; i++) {
									var childres = resource.getChildren()[i];
									if (childres.getURI()
											.indexOf("collaborate") != -1) {
										document.getElementById("type").selectedIndex = 0;
										setType();
										document.getElementById("partner").value = childres
												.getColumnValue("partner");
									} else if (childres.getURI().indexOf(
											"donate") != -1) {
										document.getElementById("type").selectedIndex = 1;
										setType();
										document.getElementById("limit").value = childres
												.getColumnValue("limit");
									} else if (childres.getURI().indexOf(
											"endowment") != -1) {
										document.getElementById("type").selectedIndex = 2;
										setType();
										document.getElementById("capital").value = childres
												.getColumnValue("capital");
										document.getElementById("rate").value = childres
												.getColumnValue("rate");
									} else if (childres.getURI().indexOf(
											"reward") != -1) {
										document.getElementById("type").selectedIndex = 3;
										setType();
										document.getElementById("reason").value = childres
												.getColumnValue("reason");
									}
								}
								document.getElementById("type").disabled = true;
							});

		} else {
			setType();
		}
		function commitPage() {
			var description = document.getElementById("description").value;
			var amount = document.getElementById("amount").value;
			var yr = document.getElementById("tYEAR");
			var mth = document.getElementById("tMON");
			var day = document.getElementById("tDAY");
			var time = yr.options[yr.selectedIndex].value + "-"
					+ mth.options[mth.selectedIndex].value + "-"
					+ day.options[day.selectedIndex].value;
			if (checkIfNull(amount) || checkIfNull(description)) {
				alert("Please fill in all the blanks!");
				return;
			}
			if (!checkIfFloat(amount)) {
				alert("Amount should be a float value!");
				return;
			}
			if (projectId != "") {
				id = document.getElementById("id").value;
				if (id == "") {
					alert("Please fill in all the blanks!");
					return;
				}
				if (!checkIfInt(id)) {
					alert("Id should be an integer value!");
					return;
				}
				resource.GET("contract/contract/" + id, function() {
					if (resource.getColumns().length != 0) {
						alert("This id exists. Please choose another id!");
						return;
					}
					switch (document.getElementById("type").selectedIndex) {
					case 0:
						postCollaborate(id, description, amount, time,
								projectId,
								document.getElementById("partner").value,
								reloadReferrer);
						break;
					case 1:
						postDonate(id, description, amount, time, projectId,
								document.getElementById("limit").value,
								reloadReferrer);
						break;
					case 2:
						postEndowment(id, description, amount, time, projectId,
								document.getElementById("capital").value,
								document.getElementById("rate").value,
								reloadReferrer);
						break;
					case 3:
						postReward(id, description, amount, time, projectId,
								document.getElementById("reason").value,
								reloadReferrer);
						break;
					}
				});
			} else if (id != "") {
				switch (document.getElementById("type").selectedIndex) {
				case 0:
					putCollaborate(id, description, amount, time, document
							.getElementById("partner").value, reloadReferrer);
					break;
				case 1:
					putDonate(id, description, amount, time, document
							.getElementById("limit").value, reloadReferrer);
					break;
				case 2:
					putEndowment(id, description, amount, time, document
							.getElementById("capital").value, document
							.getElementById("rate").value, reloadReferrer);
					break;
				case 3:
					putReward(id, description, amount, time, document
							.getElementById("reason").value, reloadReferrer);
					break;
				}
			}
		}
	</script>
</body>
</html>
